//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_drag.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);

private ["_injured","_id","_men"];
_men = nearestObjects [player, ["Man"], 2];
if (count _men > 1) then {_injured = _men select 1;};
if (format ["%1",_injured getVariable "BTC_need_revive"] != "1") exitWith {};
BTCR_dragging = true;
_injured setVariable ["BTC_dragged",1,true];

if (stance player == "PRONE") exitWith {
	[_injured] spawn FUNC(dragprone);
};

_injured attachTo [player, [0, 1.1, 0.092]];
player playMoveNow "AcinPknlMstpSrasWrflDnon";
_id = player addAction [RED_TEXT("Drop"),{(_this select 3) call FUNC(release)},[], 9, true, true, "", "true"];
//_injured playMoveNow "AinjPpneMstpSnonWrflDb_grab";
BTCR_pveh_drag = [1,_injured];publicVariable "BTCR_pveh_drag";
WaitUntil {!Alive player || ((animationstate player == "acinpknlmstpsraswrfldnon") || (animationstate player == "acinpknlmwlksraswrfldb"))};
private ["_act","_veh_selected","_array","_array_veh","_name_veh","_text_action","_action_id"];
_act = 0;_veh_selected = objNull;_array_veh = [];
while {!isNull player && alive player && !isNull _injured && alive _injured && format ["%1", _injured getVariable "BTC_need_revive"] == "1" && BTCR_dragging} do
{
	_array = nearestObjects [player, ["Air","LandVehicle"], 5];
	_array_veh = [];
	{if (_x emptyPositions "cargo" != 0) then {_array_veh = _array_veh + [_x];};} foreach _array;
	if (count _array_veh == 0) then {_veh_selected = objNull;};
	if (count _array_veh > 0 && _veh_selected != _array_veh select 0) then 
	{
		_veh_selected    = _array_veh select 0;
		_name_veh        = getText (configFile >> "cfgVehicles" >> typeof _veh_selected >> "displayName");
		_text_action     = RED_TEXT("Load wounded in " + (_name_veh));
		_action_id = player addAction [_text_action,{(_this select 3) spawn FUNC(loadIn)},[_injured,_veh_selected], 7, true, true];
		_act  = 1;
	};
	if (count _array_veh == 0 && _act == 1) then {player removeAction _action_id;_act = 0;};
	sleep 0.1;
};
if (_act == 1) then {player removeAction _action_id;};
player playMoveNow "AmovPknlMstpSrasWrflDnon";
_injured setVariable ["BTC_dragged",0,true];
if (format ["%1",_injured getVariable "BTC_need_revive"] == "1") then {detach _injured;_injured playMoveNow "AinjPpneMstpSnonWrflDb_release";};
player removeAction _id;
BTCR_dragging = false;
TRACE("End");